package com.hangwei.paperless.adapter.sub;

import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.hangwei.paperless.R;
import com.hangwei.paperless.model.bean.SubProcessListAll;
import com.hangwei.paperless.model.bean.SubProcessListInfo;
import com.hangwei.paperless.view.SealHelper;
import com.hangwei.paperless.widget.MaxRecyclerView;
import com.hangwei.paperless.widget.MyTextWatcher;
import com.twelve.all.adapter.MyAdapter;
import com.twelve.all.utils.ToastUtil;
import com.twelve.all.utils.Tools;

import java.util.List;

/**
 * Created by 张龙臣 on 2020/12/1.
 */

public abstract class SubProcessListAdapter extends MyAdapter<SubProcessListAll.DrsListBean, BaseViewHolder> {

    public SubProcessListAdapter(@Nullable List<SubProcessListAll.DrsListBean> data) {
        super(R.layout.item_sub_prosess_list, data);
    }

    public SubProcessListAdapter(@Nullable List<SubProcessListAll.DrsListBean> data, int type) {
        super(R.layout.item_sub_prosess_list_1,data);
    }

    @Override
    protected void convert(BaseViewHolder helper, final SubProcessListAll.DrsListBean item) {
        TextView tvSave = helper.getView(R.id.tvSave);
        EditText etBushNo = helper.getView(R.id.etBushNo);
        etBushNo.setText(Tools.checkedString(item.bushingSN));
        etBushNo.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                item.bushingSN = s.toString().trim();
            }
        });
        MaxRecyclerView recyclerView = helper.getView(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(new SubAdapter(item.dataList));
        helper.addOnClickListener(R.id.tvSave);
    }

    private class SubAdapter extends MyAdapter<SubProcessListInfo, BaseViewHolder> {
        public SubAdapter(List<SubProcessListInfo> dataList) {
            super(R.layout.item_item_sub_process_list, dataList);
        }

        @Override
        protected void convert(final BaseViewHolder helper, final SubProcessListInfo item) {
//            TextView tvSeq = helper.getView(R.id.tvSeq);
//            TextView tvDes = helper.getView(R.id.tvDes);
            TextView tvLimitMin = helper.getView(R.id.tvLimitMin);
            TextView tvLimitMax = helper.getView(R.id.tvLimitMax);
//            LinearLayout llLimit = helper.getView(R.id.llLimit);
            EditText etDIMMin = helper.getView(R.id.etDIMMin);
            EditText etDIMMax = helper.getView(R.id.etDIMMax);
            final EditText etActual = helper.getView(R.id.etActual);
            etDIMMin.addTextChangedListener(new MyTextWatcher() {
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    item.dbpMin = s.toString().trim();
                }
            });
            etDIMMax.addTextChangedListener(new MyTextWatcher() {
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    item.dbpMax = s.toString().trim();
                }
            });
            etActual.addTextChangedListener(new MyTextWatcher() {
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    item.actualDim = s.toString().trim();
                }
            });
            RelativeLayout rl1 = helper.getView(R.id.rl1);
            LinearLayout llLimit = helper.getView(R.id.llLimit);
            RelativeLayout rl2 = helper.getView(R.id.rl2);
//            EditText etActual = helper.getView(R.id.etActual);
            RelativeLayout flUserSeal = helper.getView(R.id.flUserSeal);
            if ("Hole I.D.".equals(item.description)) {
                llLimit.setBackgroundColor(mContext.getResources().getColor(R.color.lightGray));
                rl1.setBackgroundColor(mContext.getResources().getColor(R.color.lightGray));
                rl2.setBackgroundColor(mContext.getResources().getColor(R.color.lightGray));
                etDIMMin.setEnabled(false);
                etDIMMax.setEnabled(false);
            } else {
                llLimit.setBackgroundColor(mContext.getResources().getColor(R.color.white));
                rl1.setBackgroundColor(mContext.getResources().getColor(R.color.white));
                rl2.setBackgroundColor(mContext.getResources().getColor(R.color.white));
                etDIMMin.setEnabled(true);
                etDIMMax.setEnabled(true);
            }

            helper.setText(R.id.tvSeq, Tools.checkedString(item.seq))
                    .setText(R.id.tvDes, Tools.checkedString(item.description))
                    .setText(R.id.tvLimitMin, Tools.checkedString(item.dlMin))
                    .setText(R.id.tvLimitMax, Tools.checkedString(item.dlMax))
                    .setText(R.id.etDIMMin, Tools.checkedString(item.dbpMin))
                    .setText(R.id.etDIMMax, Tools.checkedString(item.dbpMax))
                    .setText(R.id.etActual, Tools.checkedString(item.actualDim));
            SealHelper sealHelper = new SealHelper(mContext) {
                @Override
                protected void onSeal() {
                    String text = etActual.getText().toString().trim();
                    if (TextUtils.isEmpty(text)) {
                        ToastUtil.showCenterToast("请输入实际尺寸！");
                        return;
                    }
                    seal(item, getData());
                }
            };
            sealHelper.bindData(new SealHelper.SealBean(true, item.technicianStampId, item.technicianId, item.technicianStampTypeCode, item.technicianStampCode, item.technicianDate, item.technician,true));
            flUserSeal.removeAllViews();
            flUserSeal.addView(sealHelper.getItemView());
        }
    }

    protected abstract void seal(SubProcessListInfo item, List<SubProcessListInfo> data);
}